<script>
export default {
	onLaunch: function() {
		let that = this;
	    let gps = [];
	    //保持屏幕常亮，避免手机休眠
	    wx.setKeepScreenOn({
	        keepScreenOn: true
	    });
	
	    //TODO 每隔3分钟触发自定义事件，接受系统消息
	  
	    //开启GPS后台刷新
	    uni.startLocationUpdate({
	        success(resp) {
	            console.log('开启定位成功');
	        },
	        fail(resp) {
	            console.log('开启定位失败');
	            uni.$emit('updateLocation', null);
	        }
	    });
	  
	    //GPS定位变化就自动提交给后端
	    wx.onLocationChange(function(resp) {
	        let latitude = resp.latitude;
	        let longitude = resp.longitude;
	        let speed = resp.speed;
	        // console.log(resp)
	        let location = { latitude: latitude, longitude: longitude };
	
	        let workStatus = uni.getStorageSync('workStatus');
			//上传司机GPS定位信息
	        if (workStatus == '开始接单') {
	            // TODO 只在每分钟的前10秒上报定位信息，减小服务器压力
	            // let current = new Date();
	            // if (current.getSeconds() > 10) {
	            // 	return;
	            // }
	            let settings = uni.getStorageSync('settings');
	            //TODO 先暂时写死，以后要去掉这句话
	            settings = {
	                orderDistance: 0,
	                rangeDistance: 5,
	                orientation: ''
	            }
	            let orderDistance = settings.orderDistance;
	            let rangeDistance = settings.rangeDistance;
	            let orientation = settings.orientation;
				
	            uni.request({
	                url: that.url.updateDriverLocation,
	                method: 'POST',
	                header: {
	                    token: uni.getStorageSync('token')
	                },
	                data: {
	                    latitude: latitude,
	                    longitude: longitude,
	                    orderDistance: orderDistance,
	                    rangeDistance: rangeDistance,
	                    orientateLongitude: orientation != '' ? orientation.longitude : null,
	                    orientateLatitude: orientation != '' ? orientation.latitude : null
	                },
	                success: function(resp) {
	                    if (resp.statusCode == 401) {
	                        uni.redirectTo({
	                            url: '/pages/login/login'
	                        });
	                    } else if (resp.statusCode == 200 && resp.data.code == 200) {
	                        let data = resp.data;
	                        if (data.hasOwnProperty('token')) {
	                            let token = data.token;
	                            uni.setStorageSync('token', token);
	                        }
	                        console.log("定位更新成功")
	                    } else {
	                        console.error('更新GPS定位信息失败', resp.data);
	                    }
	                },
	                fail: function(error) {
	                    console.error('更新GPS定位信息失败', error);
	                }
	            });
	        } else if (workStatus == '开始行程') {
	            //TODO 每凑够20个定位就上传一次，减少服务器的压力
	        } else if (workStatus == '接客户') {
				let executeOrder = uni.getStorageSync('executeOrder');
				let orderId = executeOrder.id;
				let data = {
					orderId: orderId,
					latitude: latitude,
					longitude: longitude
				};
				uni.request({
					url: that.url.updateOrderLocationCache,
					method: 'POST',
					header: {
						token: uni.getStorageSync('token')
					},
					data: data,
					success: function(resp) {
						if (resp.statusCode == 401) {
							uni.redirectTo({
								url: 'pages/login/login'
							});
						} else if (resp.statusCode == 200 && resp.data.code == 200) {
							let data = resp.data;
							if (data.hasOwnProperty('token')) {
								let token = data.token;
								uni.setStorageSync('token', token);
							}
							console.log('订单定位更新成功');
						} else {
							console.error('订单定位更新失败', resp.data);
						}
					},
					fail: function(error) {
						console.error('订单定位更新失败', error);
					}
				});
			} else if(workStatus == '开始行程') {
				let executeOrder = uni.getStorageSync('executeOrder');
				if (executeOrder != null) {
					gps.push({
						orderId: executeOrder.id,
						customerId: executeOrder.customerId,
						latitude: latitude,
						longitude: longitude,
						speed: speed
					});

					//把GPS定位保存到HBase中，每凑够20个定位就上传一次，减少服务器的压力
					if (gps.length == 20) {
						uni.request({
							url: that.url.insertOrderGps,
							method: 'POST',
							header: {
								token: uni.getStorageSync('token')
							},
							data: {
								list: gps
							},
							success: function(resp) {
								if (resp.statusCode == 401) {
									uni.redirectTo({
										url: '/pages/login/login'
									});
								} else if (resp.statusCode == 200 && resp.data.code == 200) {
									let data = resp.data;
									console.log("上传GPS成功");
								} else {
									console.error('保存GPS定位失败', resp.data);
								}
								gps.length = 0;
							},
							fail: function(error) {
								console.error('保存GPS定位失败', error);
							}
						});
					}
				}
			}
	        //触发自定义事件
	        uni.$emit('updateLocation', location);
	    });
	},
	onShow: function() {
		console.log('App Show');
	},
	onHide: function() {
		console.log('App Hide');
	}
};
</script>

<style lang="scss">
@import 'uview-ui/index.scss';
</style>
